var SVGNS = "http://www.w3.org/2000/svg";
var jsvgGraphicObject;

function jsvgGraphic()
{
	jsvgGraphicObject = this;
}

//------------ jsvgGraphic basic functions ------------
jsvgGraphic.prototype.Init = function()
{
	this.graphicSVG = CreateSVG( document.getElementById("graphicSVG") );
	//Clear(this.graphicSVG);
}

function CreateSVG(elem)
{
	var svgdoc = null;
	var object = document.getElementById('objectSVG');
	
	if (object.contentDocument)  // works in Mozilla
	svgdoc = object.contentDocument;
	else
	{
		try
		{
			svgdoc = object.getSVGDocument();
		}
		catch(exception)
		{
			alert(exception);
		}
	}

	var root = svgdoc.getElementsByTagName('svg')[0];

	return root;
}

function Clear(elem)
{
	if ( elem.hasChildNodes() )
	{
		while ( elem.childNodes.length >= 1 )
		{
			elem.removeChild( elem.firstChild );
		}
	}
}

jsvgGraphic.prototype.toString = function()
{
	return "[jsvgGraphic]";
}

//------------ jsvgGraphic auxiliary functions ------------
jsvgGraphic.prototype.CreateGroup = function(parent, name)
{
	var group = document.createElementNS(SVGNS, "g");
	group.setAttribute("id", group);

	if(parent == null) this.graphicSVG.appendChild(group);
	else
	parent._root.appendChild(group);

	return group;
}

jsvgGraphic.prototype.RemoveGroup = function(group)
{
	try
	{
		group.parentNode.removeChild(group);
	}
	catch(exception)
	{
		log("Error Removing : " + exception + "<br>" + group);
	}
}

jsvgGraphic.prototype.DrawLine = function(svgGraphic, x1, y1, x2, y2, color, lineWidth)
{
	var line = document.createElementNS(SVGNS, "line");
	line.setAttributeNS(null, "x1", x1);
	line.setAttributeNS(null, "y1", y1);
	line.setAttributeNS(null, "x2", x2);
	line.setAttributeNS(null, "y2", y2);
	line.setAttributeNS(null, "stroke", color);
	line.setAttributeNS(null, "stroke-width", lineWidth);
	svgGraphic.appendChild(line);
}

jsvgGraphic.prototype.DrawRectangle = function(svgGraphic, x, y, width, height, color, alpha)
{
	var rectangle = document.createElementNS(SVGNS, "rect");
	rectangle.setAttributeNS(null, "x", x);
	rectangle.setAttributeNS(null, "y", y);
	rectangle.setAttributeNS(null, "width", width);
	rectangle.setAttributeNS(null, "height", height);
	rectangle.setAttributeNS(null, "fill", color);
	rectangle.setAttributeNS(null, "fill-opacity", alpha);
	svgGraphic.appendChild(rectangle);
	
	return rectangle;
}

jsvgGraphic.prototype.DrawCircle = function(svgGraphic, x, y, r, 
  color, lineWidth, lineColor, alpha)
{
	var circle = document.createElementNS(SVGNS, "circle");
	circle.setAttributeNS(null, "cx", x);
	circle.setAttributeNS(null, "cy", y);
	circle.setAttributeNS(null, "r", r);
	circle.setAttributeNS(null, "style", "fill:" + color + ";" +
	"opacity:" + alpha + ";" +
	"stroke:" + lineColor + ";" /*+
									"stroke-width:" + lineWidth + ";"*/);
	svgGraphic.appendChild(circle);

	return circle;
}

jsvgGraphic.prototype.Translate = function(svgGraphic, dx, dy)
{
	try
	{
		var t = svgGraphic.ownerSVGElement.createSVGTransform();
		t.setTranslate(dx, dy);
		svgGraphic.transform.baseVal.appendItem(t);
	}
	catch(exception)
	{
		log("Error Translating : " + exception + "<br>" + svgGraphic + " - " + dx + " - " + dy);
	}
}

jsvgGraphic.prototype.Clone = function(svgGraphic)
{
	var c;

	try
	{
		c = svgGraphic.cloneNode(true);
	}
	catch(exception)
	{
		log("Error Cloning : " + exception);
		c = null;
	}

	return c;
}

jsvgGraphic.prototype.Move = function(svgGraphic, dest)
{
	try
	{
		dest.appendChild(svgGraphic);
	}
	catch(exception)
	{
		log("Error Moving : " + exception);
	}
}

jsvgGraphic.prototype.GetElement = function(id)
{
	var r;
	for(var i = 0; i < this.graphicSVG.childNodes.length; i++)
	{
		if(this.graphicSVG.childNodes[i].id == id)
		{
			r = this.graphicSVG.childNodes[i];
			break;
		}
	}

	return r;
}

jsvgGraphic.prototype.DrawString = function(svgGraphic, x, y, str, color, clear)
{
	if (clear) Clear(svgGraphic);
	
	var t = document.createElementNS(SVGNS, "text");
	t.setAttributeNS(null, "x", x);
	t.setAttributeNS(null, "y", y);
	t.setAttribute("style","text-anchor:start;font-size:11;font-family:Arial;fill:" + color);   
	var textN = document.createTextNode(str);   
	t.appendChild(textN); 
	svgGraphic.appendChild(t);
	
	return t;
}

//------------ movieclip ------------
function MovieClip(name, parent)
{
	this._name = name;
	this._root = jsvgGraphicObject.CreateGroup(parent, name);
	this._x = 0;
	this._y = 0;
}

MovieClip.prototype =
{
	get x()
	{
		return this._x;
	},
	set x(val)
	{
		jsvgGraphicObject.Translate(this._root, val - this._x, 0);
		this._x = val;
	},
	get y()
	{
		return this._y;
	},
	set y(val)
	{
		jsvgGraphicObject.Translate(this._root, 0, val - this._y);
		this._y = val;
	}
}

MovieClip.prototype.Dispose = function()
{
	jsvgGraphicObject.RemoveGroup(this._root);
}

MovieClip.prototype.LoadResource = function(id)
{
	log(jsvgGraphicObject.GetElement(id).id);
	var r = jsvgGraphicObject.Clone(jsvgGraphicObject.GetElement(id));
	jsvgGraphicObject.Move(r, this._root);
	
	//remuevo la transformación de translación inicial
	var t = r.ownerSVGElement.createSVGTransform();
	t.setTranslate(0, 0);
	r.transform.baseVal.replaceItem(t, 0);
}

MovieClip.prototype.DrawLine = function(x1, y1, x2, y2, color, lineWidth)
{
	jsvgGraphicObject.DrawLine(this._root, x1, y1, x2, y2, color, lineWidth);
}

MovieClip.prototype.DrawRectangle = function(x, y, width, height, color, alpha)
{
	return jsvgGraphicObject.DrawRectangle(this._root, x, y, width, height, color, alpha);
}

MovieClip.prototype.DrawCircle = function(x, y, r, color, lineWidth, lineColor, alpha)
{
	return jsvgGraphicObject.DrawCircle(this._root, x, y, r, color, lineWidth, lineColor, alpha);
}

MovieClip.prototype.DrawString = function(x, y, str, color, clear)
{
	return jsvgGraphicObject.DrawString(this._root, x, y, str, color, clear);
}

MovieClip.prototype.Clear = function()
{
	Clear(this._root);
}

MovieClip.prototype.toString = function()
{
	return "[MovieClip]";
}